using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using LitJson;

namespace KBE_HotFix.Config
{
    public class ConfigSingle<T, T1> where T : ConfigSingle<T, T1>, new()
    where T1 : class, IConfigObject, new()
    {
        private static T instance_;
        public static T Ins
        {
            get
            {
                return instance_;
            }
        }

        public ConfigSingle()
        {
            Debug.Assert(instance_ == null);
        }

        protected virtual void Init(T1[] array)
        {

        }

        protected virtual void InitSingle(T1 template)
        {

        }

        public static void Load(JsonData data, bool isArray)
        {
            Debug.Assert(instance_ == null);
            Type t1 = typeof(T1);
            ///
            ///！！！！ILRuntime 未实现 MakeGenericMethod 方法
            ///
            //MethodInfo methodInfo = t1.GetMethod("Load");
            //if(methodInfo == null)
            //{
            //    methodInfo = t1.BaseType.GetMethod("Load");
            //}
            //methodInfo.MakeGenericMethod(typeof(T1));
            //object o = methodInfo.Invoke(null, new JsonData[] { data });
            instance_ = new T();
            if (isArray)
            {
                var o = JsonDataUtils.Load<T1>(data);
                instance_.Init(o);
            }
            else
            {
                var o = JsonDataUtils.LoadSingle<T1>(data);
                instance_.InitSingle(o);
            }
        }
    }
}